package com.have.blog.sprider.processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Slf4j
public class PictureProcessor implements PageProcessor{
    // 获取img标签正则
    private static final String IMGURL_REG = "<img.*?src=.*?photos.*?/>";
    // 获取src路径的正则
    private static final String IMGSRC_REG = "/photos.*?\\?";

    public String getHtml(String searchUrl) {
        InputStream in = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            URL url = new URL(searchUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
//            connection.setRequestProperty("Charsert", "UTF-8");
//            connection.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
//            connection.setRequestProperty("accept-encoding", "gzip, deflate, br");
//            connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");

            in = connection.getInputStream();
            isr = new InputStreamReader(in);
            br = new BufferedReader(isr);
            String line;
            while((line = br.readLine()) != null) {
                sb.append(line, 0, line.length());
                System.out.println("已经爬取:" + line);
                sb.append("\n");
            }
        } catch (Exception e) {
            log.error("获取图片出现异常:{}", e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (br != null) {
                    br.close();
                }
                if (isr != null) {
                    isr.close();
                }
            } catch (IOException e) {
                log.error("获取图片出现异常:{}", e.getMessage());
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    public List<String> getImageUrl(String html) {
        Pattern compile = Pattern.compile(IMGURL_REG);
        Matcher matcher = compile.matcher(html);
        List<String> listImgUrl = new ArrayList<>();
        while (matcher.find()) {
            listImgUrl.add(matcher.group());
        }
        return listImgUrl;
    }

    public List<String> getImageSrc(List<String> imageUrlList) {
        List<String> listImageSrc = new ArrayList<>();
        for (String url : imageUrlList) {
            Matcher matcher = Pattern.compile(IMGSRC_REG).matcher(url);
            while (matcher.find()) {
                listImageSrc.add("https://foter.com" + matcher.group().substring(0, matcher.group().length() - 1));
            }
        }
        return listImageSrc;
    }

    @Override
    public void process(Page page) {
        int i = 50;
        //page计数是从2开始的
        //https://foter.com/search/instant/?q=cat&page=2
        Html html = page.getHtml();
        String url = page.getRequest().getUrl();
        List<String> list = html.regex("<img.*?src=.*?photos.*?/>").all();
        List<String> imageSrc = getImageSrc(list);
        String jsonString = JSON.toJSONString(imageSrc);
        if (CollectionUtils.isEmpty(imageSrc)) {
            page.putField("imgSrc", jsonString);
            page.putField("searchKey", findSearchKey(url));
        } else {
            //跳过爬取
            page.setSkip(true);
        }
    }

    @Override
    public Site getSite() {
        return Site.me().setCharset("utf8").setRetryTimes(2).setSleepTime(2000).setTimeOut(4000);
    }
    /**
     * 查找输入的关键词
     *
     * @param url
     * @return
     */
    public static String findSearchKey(String url) {
        // 按指定模式在字符串查找
        String reg = "\\?q=.*";
        // 创建 Pattern 对象
        // 现在创建 matcher对象 匹配的模式
        Pattern pattern = Pattern.compile(reg);
        Matcher m = pattern.matcher(url);
        if (m.find()) {
            String group = m.group(0);
            String replace = group.replace("?q=", "").replaceFirst("&.*", "");
            return replace;
        } else {
            return url;
        }
    }

}
